<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="queryForms">
			<el-form-item label="产品名称" prop="productName">
				<el-input maxlength="20" show-word-limit  v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品型号" prop="productModel">
				<el-input maxlength="20" show-word-limit  v-model="queryParams.productModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="送修日期" prop="receiptTime">
				<el-date-picker v-model="queryParams.receiptTime" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"> </el-date-picker>
			</el-form-item>
			<el-form-item label="任务单号" prop="repairNumber">
				<el-input maxlength="20" show-word-limit  v-model="queryParams.repairNumber" placeholder="请输入任务单号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="出入库状态" prop="outPutWarehouseStatus" label-width="90px">
				<el-select v-model="queryParams.outPutWarehouseStatus" placeholder="请选择">
					<el-option label="未入库" value="0"> </el-option>
					<el-option label="已出库" value="1"> </el-option>
					<el-option label="已入库" value="2"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" size="mini" @click="handleUpdate(1)" v-hasPermi="['erp:house:input']">入库</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" size="mini" @click="handleUpdate(2)" v-hasPermi="['erp:house:out']">出库</el-button>
			</el-col>
			<CreateForm :ids="ids" formtype="t_warehouse" />
			<right-toolbar menuNames="Warehouse62" @changeTableData="changeTableData" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns" perm="synthesize:store:Warehouse:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="productList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="任务单号" prop="repairNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="送修单位" prop="clientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="产品名称" prop="productName" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="产品型号" prop="productModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<el-table-column label="产品编号" prop="productNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="出入库状态" prop="outPutWarehouseStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[8].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.outPutWarehouseStatus == 0">未入库</div>
						<div v-if="scope.row.outPutWarehouseStatus == 1">已出库</div>
						<div v-if="scope.row.outPutWarehouseStatus == 2">已入库</div>
					</template>
				</el-table-column>
        <el-table-column label="数量" prop="productQuantity" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="送修日期" prop="receiptTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" />
				<el-table-column label="军检日期" prop="armyExamineTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible" />
				<el-table-column label="入库日期" prop="putTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[9].visible" />
				<el-table-column label="出库日期" prop="outTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[10].visible" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(1, scope.row)" v-hasPermi="['erp:house:input']">入库</el-button>
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(2, scope.row)" v-hasPermi="['erp:house:out']">出库</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination67" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改产品列对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="出库日期" prop="outTime" v-if="type == 2">
					<el-date-picker v-model="form.outTime" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"> </el-date-picker>
				</el-form-item>
				<el-form-item label="入库日期" prop="putTime" v-if="type == 1">
					<el-date-picker v-model="form.putTime" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"> </el-date-picker>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import CreateForm from "@/views/components/CreateForm"
import { repairProductId, outPutWarehouse1, repairProductList, outPutWarehouse } from "@/api/synthesize/store/Warehouse"
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
export default {
	name: "Warehouse",
	dicts: ["default_status", "default_product"],
	components: { CreateForm },
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			type: 1,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 导入产品
			ImportOpen: false,
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 产品列表格数据
			productList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				productName: null,
				productModel: null,
				receiptTime: null,
				repairNumber: null,
				outPutWarehouseStatus: null
			},
			// 表单参数
			form: { state: "1" },
			// 表单校验
			rules: {
				outTime: [{ required: true, message: "出库日期不能为空", trigger: "blur" }],
				putTime: [{ required: true, message: "入库日期不能为空", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `任务单号`, visible: true },
				{ key: 1, label: `送修单位`, visible: true },
				{ key: 2, label: `产品名称`, visible: true },
				{ key: 3, label: `产品型号`, visible: true },
				{ key: 4, label: `产品编号`, visible: true },
				{ key: 5, label: `数量`, visible: true },
				{ key: 6, label: `送修日期`, visible: true },
				{ key: 7, label: `军检日期`, visible: true },
				{ key: 8, label: `出入库状态`, visible: true },
				{ key: 9, label: `入库日期`, visible: true },
				{ key: 10, label: `出库如期`, visible: true }
			]
		}
	},
	created() {
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination67" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		handleInput() {},
		handleOutput() {},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		closeOpen(e) {
			this.ImportOpen = false
			if (e) {
				this.getList()
			}
		},
		/** 查询产品列列表 */
		getList() {
			this.loading = true
			repairProductList(this.queryParams).then(response => {
				this.productList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				outTime: undefined,
				putTime: undefined,
				productName: null,
				productModel: null,
				receiptTime: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 修改按钮操作 */
		handleUpdate(status, row) {
			this.reset()
			if (row) {
				repairProductId(row.id).then(response => {
					this.form = response.data
				})
			}
			if (status == 1) {
				this.type = 1
				this.title = "入库日期"
			} else {
				this.type = 2
				this.title = "出库日期"
			}
			this.open = true
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id) {
						this.form.outPutWarehouseStatus = this.type == 1 ? 2 : 1
						outPutWarehouse([this.form]).then(response => {
							this.$modal.msgSuccess(this.type == 1 ? "入库成功" : "出库成功")
							this.open = false
							this.getList()
						})
					} else {
						outPutWarehouse1({
							outPutWarehouseStatus: this.type == 1 ? 2 : 1,
							ids: this.ids,
							outTime: this.form.outTime,
							putTime: this.form.putTime
						}).then(res => {
							this.$modal.msgSuccess(this.type == 1 ? "入库成功" : "出库成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/product/export",
				{
					...this.queryParams
				},
				`product_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 152px !important;
}
</style>
